<html>
<!-- =====================================================================

  File:      Generated file for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     June 16, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
<head>
  <link rel=stylesheet href=style.css>
</head>
<body>
<div class=SourcePanel style="font-size:12">
<pre style="background-color:white">
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Collections</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">ComponentModel</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Data</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Drawing</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">Security</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">UI</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">UI</span>.<span style="color: #2040a0">WebControls</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">UI</span>.<span style="color: #2040a0">HtmlControls</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Text</span><span style="color: #4444FF">;</span>
<span style="color: #0000FF"><strong>using</strong></span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Text</span>.<span style="color: #2040a0">RegularExpressions</span><span style="color: #4444FF">;</span>

<span style="color: #008000">/* =====================================================================

  File:      Login.aspx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Authenticates the user
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */</span>

<span style="color: #0000FF"><strong>namespace</strong></span> <span style="color: #2040a0">Microsoft</span>.<span style="color: #2040a0">Samples</span>.<span style="color: #2040a0">SqlServer</span> <span style="color: #4444FF"><strong>{</strong></span>

    <span style="color: #0000FF"><strong>public</strong></span> <span style="color: #2040a0">partial</span> <span style="color: #0000FF"><strong>class</strong></span> <span style="color: #2040a0">Login</span> <span style="color: #4444FF">:</span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">UI</span>.<span style="color: #2040a0">Page</span>
    <span style="color: #4444FF"><strong>{</strong></span>

    
<span style="color: #008000">//         public Login() {</span>
<span style="color: #008000">//             Page.Init += new System.EventHandler(Page_Init);</span>
<span style="color: #008000">//         }</span>

        <span style="color: #008000">//*******************************************************</span>
        <span style="color: #008000">//</span>
        <span style="color: #008000">// The LoginButton_Click event is used on this page to</span>
        <span style="color: #008000">// authenticate a customer's supplied username/password</span>
        <span style="color: #008000">// credentials against a database.</span>
        <span style="color: #008000">//</span>
        <span style="color: #008000">// If the supplied username/password are valid, then</span>
        <span style="color: #008000">// the event handler adds a cookie to the client</span>
        <span style="color: #008000">// (so that we can personalize the home page's welcome</span>
        <span style="color: #008000">// message), migrates any items stored in the user's</span>
        <span style="color: #008000">// temporary (non-persistent) shopping cart to their</span>
        <span style="color: #008000">// permanent customer account, and then redirects the browser</span>
        <span style="color: #008000">// back to the originating page.</span>
        <span style="color: #008000">//</span>
        <span style="color: #008000">//*******************************************************</span>

        

        <span style="color: #0000FF"><strong>private</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">Page_Load</span><span style="color: #4444FF">(</span><span style="color: #2040a0">object</span> <span style="color: #2040a0">sender</span>, <span style="color: #2040a0">System</span>.<span style="color: #2040a0">EventArgs</span> <span style="color: #2040a0">e</span><span style="color: #4444FF">)</span> <span style="color: #4444FF"><strong>{</strong></span>
            <span style="color: #008000">// Put user code to initialize the page here</span>
			<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">Focus</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
        <span style="color: #4444FF"><strong>}</strong></span>

        <span style="color: #0000FF"><strong>private</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">Page_Init</span><span style="color: #4444FF">(</span><span style="color: #2040a0">object</span> <span style="color: #2040a0">sender</span>, <span style="color: #2040a0">EventArgs</span> <span style="color: #2040a0">e</span><span style="color: #4444FF">)</span> <span style="color: #4444FF"><strong>{</strong></span>
            <span style="color: #008000">//</span>
            <span style="color: #008000">// CODEGEN: This call is required by the ASP.NET Web Form Designer.</span>
            <span style="color: #008000">//</span>
            <span style="color: #2040a0">InitializeComponent</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
        <span style="color: #4444FF"><strong>}</strong></span>

<span style="color: #0000ff"><strong>		#region Web Form Designer generated code</strong></span>
        <span style="color: #008000">/// &lt;summary&gt;</span>
        <span style="color: #008000">/// Required method for Designer support - do not modify</span>
        <span style="color: #008000">/// the contents of this method with the code editor.</span>
        <span style="color: #008000">/// &lt;/summary&gt;</span>
        <span style="color: #0000FF"><strong>private</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">InitializeComponent</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span> <span style="color: #4444FF"><strong>{</strong></span>    

		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #0000FF"><strong>bool</strong></span> <span style="color: #2040a0">IsValidEmail</span><span style="color: #4444FF">(</span><span style="color: #2040a0">string</span> <span style="color: #2040a0">strIn</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Return true if strIn is in valid e-mail format.</span>
			<span style="color: #0000FF"><strong>return</strong></span> <span style="color: #2040a0">Regex</span>.<span style="color: #2040a0">IsMatch</span><span style="color: #4444FF">(</span><span style="color: #2040a0">strIn</span>,
								 @<span style="color: #444444">&quot;^([<span style="color: #77dd77">\w</span>-<span style="color: #77dd77">\.</span>]+)@((<span style="color: #77dd77">\[</span>[0-9]{1,3}<span style="color: #77dd77">\.</span>[0-9]{1,3}<span style="color: #77dd77">\.</span>[0-9]{1,3}<span style="color: #77dd77">\.</span>)|(([<span style="color: #77dd77">\w</span>-]+<span style="color: #77dd77">\.</span>)+))([a-zA-Z]{2,4}|[0-9]{1,3})(<span style="color: #77dd77">\]</span>?)$&quot;</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
		<span style="color: #4444FF"><strong>}</strong></span>

		<span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">OnBeforeLogin</span><span style="color: #4444FF">(</span><span style="color: #2040a0">object</span> <span style="color: #2040a0">sender</span>, <span style="color: #2040a0">CancelEventArgs</span> <span style="color: #2040a0">e</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #4444FF">!</span><span style="color: #2040a0">IsValidEmail</span><span style="color: #4444FF">(</span><span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">UserName</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">InstructionText</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;Enter a valid e-mail address.&quot;</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">InstructionTextStyle</span>.<span style="color: #2040a0">ForeColor</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Drawing</span>.<span style="color: #2040a0">Color</span>.
														<span style="color: #2040a0">RosyBrown</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">e</span>.<span style="color: #2040a0">Cancel</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
			<span style="color: #0000FF"><strong>else</strong></span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">InstructionText</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">String</span>.<span style="color: #2040a0">Empty</span><span style="color: #4444FF">;</span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>


        <span style="color: #0000FF"><strong>protected</strong></span> <span style="color: #0000FF"><strong>void</strong></span> <span style="color: #2040a0">Login1_Authenticate</span><span style="color: #4444FF">(</span><span style="color: #2040a0">object</span> <span style="color: #2040a0">sender</span>, <span style="color: #2040a0">AuthenticateEventArgs</span> <span style="color: #2040a0">e</span><span style="color: #4444FF">)</span>
		<span style="color: #4444FF"><strong>{</strong></span>
			<span style="color: #008000">// Only attempt a login if all form fields on the page are valid</span>
			<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">Page</span>.<span style="color: #2040a0">IsValid</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span><span style="color: #4444FF">)</span>
			<span style="color: #4444FF"><strong>{</strong></span>
				<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">InstructionText</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;&quot;</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">String</span> <span style="color: #2040a0">previousLogin</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">System</span>.<span style="color: #2040a0">Web</span>.<span style="color: #2040a0">HttpContext</span>.<span style="color: #2040a0">Current</span>.<span style="color: #2040a0">User</span>.
									   <span style="color: #2040a0">Identity</span>.<span style="color: #2040a0">Name</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">// Save old ShoppingCartID</span>
				<span style="color: #2040a0">ShoppingCartDB</span> <span style="color: #2040a0">shoppingCart</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">ShoppingCartDB</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">String</span> <span style="color: #2040a0">tempCartID</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">shoppingCart</span>.<span style="color: #2040a0">GetShoppingCartId</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

				<span style="color: #008000">// Attempt to Validate User Credentials using CustomersDB</span>
				<span style="color: #2040a0">CustomersDB</span> <span style="color: #2040a0">accountSystem</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">CustomersDB</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">CustomerDetails</span> <span style="color: #2040a0">customerDetails</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">accountSystem</span>.<span style="color: #2040a0">Login</span><span style="color: #4444FF">(</span><span style="color: #2040a0">Login1</span>.
																	  <span style="color: #2040a0">UserName</span>,
																	  <span style="color: #2040a0">Login1</span>.
																	  <span style="color: #2040a0">Password</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #2040a0">String</span> <span style="color: #2040a0">customerId</span> <span style="color: #4444FF">=</span> <span style="color: #4444FF">(</span><span style="color: #2040a0">customerDetails</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span> ?
									<span style="color: #2040a0">null</span> <span style="color: #4444FF">:</span> <span style="color: #2040a0">customerDetails</span>.<span style="color: #2040a0">CustomerID</span><span style="color: #4444FF">;</span>

				<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">customerId</span> <span style="color: #4444FF">!</span><span style="color: #4444FF">=</span> <span style="color: #2040a0">null</span><span style="color: #4444FF">)</span>
				<span style="color: #4444FF"><strong>{</strong></span>
					<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">previousLogin</span>.<span style="color: #2040a0">Length</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #FF0000">0</span><span style="color: #4444FF">)</span>

						<span style="color: #008000">// Migrate any existing shopping cart items into the permanent shopping cart</span>
						<span style="color: #2040a0">shoppingCart</span>.<span style="color: #2040a0">MigrateCart</span><span style="color: #4444FF">(</span><span style="color: #2040a0">tempCartID</span>, <span style="color: #2040a0">customerId</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

					<span style="color: #008000">// Store the user's fullname in a cookie for personalization purposes</span>
					<span style="color: #2040a0">String</span> <span style="color: #2040a0">fullName</span> <span style="color: #4444FF">=</span> <span style="color: #2040a0">customerDetails</span>.<span style="color: #2040a0">FirstName</span> <span style="color: #4444FF">+</span> <span style="color: #444444">&quot; &quot;</span> <span style="color: #4444FF">+</span>
									  <span style="color: #2040a0">customerDetails</span>.<span style="color: #2040a0">LastName</span><span style="color: #4444FF">;</span>
					<span style="color: #2040a0">UnicodeEncoding</span> <span style="color: #2040a0">unicode</span> <span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>new</strong></span> <span style="color: #2040a0">UnicodeEncoding</span><span style="color: #4444FF">(</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

					<span style="color: #2040a0">Response</span>.<span style="color: #2040a0">Cookies</span><span style="color: #4444FF">[</span><span style="color: #444444">&quot;AdventureWorks_FullName&quot;</span><span style="color: #4444FF">]</span>.<span style="color: #2040a0">Value</span> <span style="color: #4444FF">=</span>
					<span style="color: #2040a0">Convert</span>.
																		<span style="color: #2040a0">ToBase64String</span><span style="color: #4444FF">(</span><span style="color: #2040a0">unicode</span>.
																					   <span style="color: #2040a0">GetBytes</span><span style="color: #4444FF">(</span><span style="color: #2040a0">fullName</span><span style="color: #4444FF">)</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>

					<span style="color: #008000">// Make the cookie persistent only if the user selects &quot;persistent&quot; login checkbox</span>
					<span style="color: #0000FF"><strong>if</strong></span> <span style="color: #4444FF">(</span><span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">RememberMeSet</span> <span style="color: #4444FF">=</span><span style="color: #4444FF">=</span> <span style="color: #0000FF"><strong>true</strong></span><span style="color: #4444FF">)</span>
					<span style="color: #4444FF"><strong>{</strong></span>
						<span style="color: #2040a0">Response</span>.<span style="color: #2040a0">Cookies</span><span style="color: #4444FF">[</span><span style="color: #444444">&quot;AdventureWorks_FullName&quot;</span><span style="color: #4444FF">]</span>.<span style="color: #2040a0">Expires</span> <span style="color: #4444FF">=</span>
						<span style="color: #2040a0">DateTime</span>.
																			  <span style="color: #2040a0">Now</span>.
																			  <span style="color: #2040a0">AddMonths</span><span style="color: #4444FF">(</span><span style="color: #FF0000">1</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
					<span style="color: #4444FF"><strong>}</strong></span>

					<span style="color: #008000">// Redirect browser back to originating page</span>
					<span style="color: #2040a0">FormsAuthentication</span>.<span style="color: #2040a0">RedirectFromLoginPage</span><span style="color: #4444FF">(</span><span style="color: #2040a0">customerId</span>,
															  <span style="color: #2040a0">Login1</span>.
															  <span style="color: #2040a0">RememberMeSet</span><span style="color: #4444FF">)</span><span style="color: #4444FF">;</span>
				<span style="color: #4444FF"><strong>}</strong></span>
				<span style="color: #0000FF"><strong>else</strong></span>
				<span style="color: #4444FF"><strong>{</strong></span>
					<span style="color: #2040a0">Login1</span>.<span style="color: #2040a0">InstructionText</span> <span style="color: #4444FF">=</span> <span style="color: #444444">&quot;Login Failed!&quot;</span><span style="color: #4444FF">;</span>
				<span style="color: #4444FF"><strong>}</strong></span>
			<span style="color: #4444FF"><strong>}</strong></span>
		<span style="color: #4444FF"><strong>}</strong></span>

<span style="color: #0000ff"><strong>		#endregion</strong></span>

    <span style="color: #4444FF"><strong>}</strong></span>
<span style="color: #4444FF"><strong>}</strong></span>

</pre>
